class staffItem {
    constructor(item){
        this.info = {};
        this.info.name = item.name;
        this.info.age = item.age || 0;
        this.info.sex = item.sex;
        this.info.id = item.id;
        this.info.descrip = item.descrip || '';
        this.key = ++staffItem.key;
    }
}
staffItem.key = 0;

export default class Staff {

    constructor(){
        this.allStaff = [
            new staffItem(Staff.rawData[0]),
            new staffItem(Staff.rawData[1]),
            new staffItem(Staff.rawData[2]),
            new staffItem(Staff.rawData[3]),
            new staffItem(Staff.rawData[4]),
            new staffItem(Staff.rawData[5]),
            new staffItem(Staff.rawData[6]),
            new staffItem(Staff.rawData[7]),
            new staffItem(Staff.rawData[8]),
            new staffItem(Staff.rawData[9]),
            new staffItem(Staff.rawData[10])
        ];
        this.staff = this.allStaff;
        this.word = '';  //搜索关键字
    }

    //增
    addStaffItem(item) {
        let newItem = new staffItem(item);
        this.allStaff.push(newItem);
        this.staff = this.allStaff;
        return this;
    }

    //搜索
	_searchStaff(word){
	    this.word = word;
	    //在staff中搜索
	    this.staff = this.staff.filter(item => {
		    return item.info.name.indexOf(word)!=-1 || 
			       (item.info.age+'').indexOf(word)!=-1 || 
				   item.info.id.indexOf(word)!=-1 ||
				   item.info.sex.indexOf(word)!=-1;
		});
	}   

    //筛选
	_filtStaff(filtType){
	    this.filtType = filtType;
	    switch(parseInt(filtType)){
		    case 0: 
			    this.staff = this.allStaff;
				break;
			case 1: 
			    this.staff = this.allStaff.filter(item => {
				    return item.info.id == '主任';
				});
				break;
			case 2: 
			    this.staff = this.allStaff.filter(item => {
				    return item.info.id == '老师';
				});
				break;
			case 3: 
			    this.staff = this.allStaff.filter(item => {
				    return item.info.id == '学生';
				});
				break;
			case 4: 
			    this.staff = this.allStaff.filter(item => {
				    return item.info.id == '实习';
				});
				break;
			default: break;
		}
    };

    //搜索
    searchStaff(word){
	    this._filtStaff(this.filtType);
	    this._searchStaff(word);
		return this;
	}
    
    //
    filtStaff(filtType){
	    this._filtStaff(filtType);
		this._searchStaff(this.word);
		return this;
    }
    
    //删
	removeStaffItem(key) {
	    let newStaff = this.allStaff.filter(item => {
		    return item.key != key;
		});
		this.allStaff = newStaff;
		//筛选 搜多过滤
		this._filtStaff(this.filtType);
		this._searchStaff(this.word);
		return this;
	}
}

Staff.rawData = [{ descrip:'我是一匹来自远方的狼。', sex: '男', age: 20, name: '张三', id: '主任'},
                 { descrip:'我是一匹来自远方的狼。', sex: '女', age: 21, name: '赵静', id: '学生'},
                 { descrip:'我是一匹来自远方的狼。', sex: '女', age: 22, name: '王二麻', id: '学生'},
                 { descrip:'我是一匹来自远方的狼。', sex: '女', age: 24, name: '李晓婷', id: '实习'},
                 { descrip:'我是一匹来自远方的狼。', sex: '男', age: 23, name: '张春田', id: '实习'},
                 { descrip:'我是一匹来自远方的狼。', sex: '男', age: 22, name: '刘建国', id: '学生'},
                 { descrip:'我是一匹来自远方的狼。', sex: '男', age: 24, name: '张八', id: '主任'},
                 { descrip:'我是一匹来自远方的狗。', sex: '男', age: 35, name: '李四', id: '老师'},
                 { descrip:'我是一匹来自远方的猪。', sex: '男', age: 42, name: '王五', id: '学生'},
                 { descrip:'我是一匹来自远方的牛。', sex: '男', age: 50, name: '赵六', id: '实习'},
                 { descrip:'我是一匹来自远方的马。', sex: '男', age: 60, name: '孙七', id: '实习'}];